package paa.simulator.grafo;

import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;

public class VideoPool {

	private static VideoPool instance;
	private Map<Integer, Video> pool;

	private VideoPool() {
		this.pool = new WeakHashMap<Integer, Video>();
	}
	
	public static VideoPool getInstance() {
		
		if (instance == null) {
			instance = new VideoPool();
		}
		
		return instance;
	}
	
	public Video getVideo(Integer id) {
		Video video = this.pool.get(id);
		return video;
	}
	
	public Video putVideo(Integer id, Set<Integer> tags) {
		if (pool.containsKey(id)) {
			throw new RuntimeException("Repeated Video " + id);
		}
		
		Video video = new Video(id, tags);
		this.pool.put(id, video);
		return video;
	}
	
	public Video overwriteVideo(Integer id, Set<Integer> tags) {
		Video video = this.pool.get(id);
		
		if (video != null) {
			video.setID(id);
			video.setTags(tags);
		} else {
			video = new Video(id, tags);
			this.pool.put(id, video);
		}
		
		return video;
	}
	
	public Video removeVideo(Integer id) {
		return this.pool.remove(id);
	}
	
	public class Video {

		private int id;
		private Set<Integer> tags;

		private Video(int id, Set<Integer> tags) {
			this.id = id;
			this.tags = tags;
		}

		public Set<Integer> getTags() {
			return tags;
		}
		
		public int getID() {
			return id;
		}
		
		private void setID(int id) {
			this.id = id;
		}
		
		private void setTags(Set<Integer> tags) {
			this.tags = tags;
		}
		
		@Override
		public int hashCode() {
			return id;
		}
		
		@Override
		public boolean equals(Object obj) {
			if (obj instanceof Video) {
				Video v = (Video) obj;
				return v.id == id;
			}
			
			return false;
		}
	}

	public void clear() {
		this.pool.clear();
	}
	
}
